public class Demo13 {
    public static void main(String[] args) throws InterruptedException {
        Object block1 = new Object();
        Object block2 = new Object();
        Object block3 = new Object();

        Thread thread1 = new Thread(() -> {
                try {
                    for (int i = 0; i < 10; i++) {
                        synchronized (block1) {
                            block1.wait();
                        }
                        System.out.print("A");
                        synchronized (block2) {
                            block2.notify();
                        }
                    }
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread thread2 = new Thread(() -> {
                try {
                    for (int i = 0; i < 10; i++) {
                        synchronized (block2) {
                            block2.wait();
                        }
                        System.out.print("B");
                        synchronized (block3) {
                            block3.notify();
                        }
                    }
            }catch (InterruptedException e) {
                    e.printStackTrace();
                }

        });
        Thread thread3 = new Thread(() -> {
                try {
                    for (int i = 0; i < 10; i++) {
                        synchronized (block3) {
                            block3.wait();
                        }
                        System.out.println("C");
                        synchronized (block1) {
                            block1.notify();
                        }
                    }
            }catch (InterruptedException e) {
                    e.printStackTrace();
                }

        });

        thread1.start();
        thread2.start();
        thread3.start();

        Thread.sleep(1000);
        synchronized (block1) {
            block1.notify();
        }
    }
}
